<script type="text/javascript">
    function commit()
    {
        var faction = document.getElementById("faction");
        var form = document.getElementById("fr");
        if(faction.value == 'add')
            form.action += 'add';
        else
            form.action += 'edit/'  <?php if(isset($id)) echo "+ " . $id; else echo ""?>;
        form.submit();
    }
</script>
<fieldset>
    <legend>Thông tin danh mục</legend>
    <span id="message" style="color:red;font-weight:bold;background-color:#f0f8ff;">
        <?php
         if(isset($error))
         {
            foreach($error as $err)
            {
                echo "* $err <br />";
            }
         }

        if(isset($message))
        {
            echo "* $message <br />";
        }
        echo "<br/>";
        ?>
    </span>
    <form name="fr" id="fr" action="<?php echo Utility::getSiteUrl();?>/category/" method="post">
        <input type="hidden"  name="faction" id="faction" value="<?php  echo $action;?>"/>
    <table>
        <tr>
            <td style="width:90px">Tên danh mục</td>
            <td><input type="text" name="name" value="<?php if($action == 'edit') if(isset($input)) echo $input->name;?>"/></td>
        </tr>
        <tr>
            <td>Đường dẫn</td>
            <td><input type="text" name="link" value="<?php if($action == 'edit') if(isset($input)) echo $input->link;?>"/></td>
        </tr>
        <tr>
            <td>Danh mục cha</td>
            <td>
                <select name="parent_id">
                    <option value="0">Chọn danh mục</option>
                    <?php
                    if(isset($cat_list))
                    {
                        if($action == 'edit')
                        {
                            foreach($cat_list as $cat)
                            {
                                if(isset($id) && $id == $cat->id) continue;
                                echo "<option value='$cat->id'";
                                if(isset($input) && $input->parent_id == $cat->id) echo "selected='selected'";
                                echo ">";
                                for($i = 0; $i < $cat->c_level; $i++) echo "--";
                                echo " $cat->name</option>";
                            }
                        }
                        else
                        {
                            foreach($cat_list as $cat)
                            {
                            if(isset($id) && $id == $cat->id) continue;
                            echo "<option value='$cat->id'";
                            if(isset($input) && $input['parent_id'] == $cat->id) echo "selected='selected'";
                            echo ">";
                            for($i = 0; $i < $cat->c_level; $i++) echo "--";
                            echo " $cat->name</option>";
                            }
                        }

                    }
                    ?>
                    
                </select>
            </td>
        </tr>
        <tr>
            <td></td>
            <td>
                <input type="button"  value="<?php if($action =='add') echo "Thêm"; else echo "Sửa";?>" onclick="commit()" />
                <input type="reset" value="Nhập lại" />
                <input type="button" value="Về trang chính" onclick="window.location='<?php echo Utility::getSiteUrl() . '/category/';?>'"/>
            </td>
        </tr>
    </table>
    </form>
</fieldset>